package com.ocom.payconfig.controller;

import com.ocom.common.constant.PayConfigUrl;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.payconfig.FeeItemListVo;
import com.ocom.common.vo.payconfig.FeeItemXMVo;
import com.ocom.payconfig.service.FeeItemService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * fee_itemController
 *
 * @author junShu
 * @date 2023-08-18 17:02:55
 */
@RestController
public class FeeItemXMController {

    @Resource
    private FeeItemService feeItemService;

    /**
     * 查询FeeItem列表
     */
    @PostMapping(PayConfigUrl.URL_feeItemListXM)
    public Result feeItemListXm(@RequestBody FeeItemListVo request) {
        return ResultUtil.success(feeItemService.feeItemListXM(request.getComId(), request));
    }

    /**
     * 新增保存FeeItem
     */
    @PostMapping(PayConfigUrl.URL_newFeeItemXM)
    public Result newFeeItemXM(@RequestBody FeeItemXMVo request) {
        //周 月 时间范围判断
        String[] parts = request.getDateList().split(",");
        List<String> dayList1 = Arrays.asList(parts);
        if(request.getExecutionPlan() == 1){
            //本周日期判断
            // 获取当前日期
            String dateString = dayList1.get(0);
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate today =LocalDate.parse(dateString, formatter);
            // 获取本周的第一天（周一）
            LocalDate monday = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));

            //七天日期
            List<String> dateList = new ArrayList<>();
            for (int i = 0; i < 7; i++) {
                LocalDate day = monday.plusDays(i);
                String day2 = day.format(formatter);
                dateList.add(day2);
            }

            for (String day:dayList1){
                if(!dateList.contains(day)){
                    return ResultUtil.error(ResultEnum.Err_4153);
                }
            }

        }else{
            String month = dayList1.get(0).substring(0,7);
            for (String day:dayList1){
                if(!day.startsWith(month)){
                    return ResultUtil.error(ResultEnum.Err_4154);
                }
            }
        }

        return feeItemService.newFeeItemXM(request.getComId(), request);
    }

    /**
     * 修改保存FeeItem
     */
    @PostMapping(PayConfigUrl.URL_updateFeeItemXM)
    public Result updateFeeItemXM(@RequestBody FeeItemXMVo request) {

        //周 月 时间范围判断
        String[] parts = request.getDateList().split(",");
        List<String> dayList1 = Arrays.asList(parts);
        if(request.getExecutionPlan() == 1){
            //本周日期判断
            // 获取当前日期
            String dateString = dayList1.get(0);
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate today =LocalDate.parse(dateString, formatter);
            // 获取本周的第一天（周一）
            LocalDate monday = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));

            //七天日期
            List<String> dateList = new ArrayList<>();
            for (int i = 0; i < 7; i++) {
                LocalDate day = monday.plusDays(i);
                String day2 = day.format(formatter);
                dateList.add(day2);
            }

            for (String day:dayList1){
                if(!dateList.contains(day)){
                    return ResultUtil.error(ResultEnum.Err_4153);
                }
            }

        }else{
            String month = dayList1.get(0).substring(0,7);
            for (String day:dayList1){
                if(!day.startsWith(month)){
                    return ResultUtil.error(ResultEnum.Err_4154);
                }
            }
        }

        return feeItemService.updateFeeItemXM(request.getComId(), request);
    }





}
